﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yiCommerce.Areas.Admin.Models.Orders;
using yiCommerce.Attributes;
using yiCommerce.Controllers;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Orders;
using yiCommerce.Core.Domain.Payments;
using yiCommerce.Core.Domain.Securitys;
using yiCommerce.Core.Domain.Shippings;
using yiCommerce.Service.Orders;
using yiCommerce.Service.Securitys;

namespace yiCommerce.Areas.Admin.Controllers
{
    [AdminAuthorize(Order = 1)]
    [AdminOrderAuthorize(Order = 2)]
    public class OrderController : BaseController
    {
        private IPermissionService _permissionService;
        private IOrderService _orderService;
        private IWorkContext _workContext;

        public OrderController(IWorkContext workContext,IPermissionService permissionService, IOrderService orderService)
        {
            _workContext = workContext;
            _permissionService = permissionService;
            _orderService = orderService;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

       
        private List<SelectItem> ConvertOrderStatusToSelectItemList(IEnumerable<OrderStatus> orderStatuses)
        {
            return orderStatuses.Select(orderstatus => new SelectItem()
                {
                    Value = (int) orderstatus, Text = orderstatus.ToDesc()
                }).ToList();
        }


        private List<SelectItem> ConvertPaymentStatusToSelectItemList(IEnumerable<PaymentStatus> paymentStatus)
        {
            return paymentStatus.Select(p=> new SelectItem()
            {
                Value = (int)p,
                Text = p.ToDesc()
            }).ToList();
        }
        private List<SelectItem> ConvertShippingStatusToSelectItemList(IEnumerable<ShippingStatus> shippingStatus)
        {
            return shippingStatus.Select(s=> new SelectItem()
            {
                Value = (int)s,
                Text = s.ToDesc()
            }).ToList();
        }

        public ActionResult List()
        {


            var model = new OrderSearchFilterModel()
            {
                OrderStatus = OrderStatus.Processing,
                OrderStatusId = (int)OrderStatus.Processing,
                PaymentStatus = PaymentStatus.Pending,
                PaymentStatusId = (int)PaymentStatus.Pending,
                ShippingStatus = ShippingStatus.NotYetShipped,
                ShippingStatusId = (int)ShippingStatus.NotYetShipped,
                AvailableOrderStatuses = ConvertOrderStatusToSelectItemList(Enum.GetValues(typeof(OrderStatus)).Cast<OrderStatus>()),
                AvailablePaymentStatuses = ConvertPaymentStatusToSelectItemList(Enum.GetValues(typeof(PaymentStatus)).Cast<PaymentStatus>()),
                AvailableShippingStatuses = ConvertShippingStatusToSelectItemList(Enum.GetValues(typeof(ShippingStatus)).Cast<ShippingStatus>()),
            };

          

            //load orders
            var pageIndex = 0;
            int pageSize = 100;
            var orders = _orderService.SearchOrders(0,
                    this.GetShopId(),
                    model.StartDate,
                    model.EndDate,
                    model.OrderStatus,
                    model.PaymentStatus,
                    model.ShippingStatus,
                    pageIndex,
                    pageSize);

            var resultModel= new OrderSearchModel {Orders = orders, Filter = model};
            ViewBag.Model = resultModel; 

         
            return View();

        }

     
    }
}
